package com.linewow.xhyy.nihaonew.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.adapter.ShieldAdapter;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.base.BaseInfo;
import com.linewow.xhyy.nihaonew.entity.NiHaoGeneral;
import com.linewow.xhyy.nihaonew.entity.user.SimpleUserInfo;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/11/2.
 */
public class ShieldActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {
    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.toolbar_back_right_tv)
    TextView toolbarBackRightTv;
    @Bind(R.id.toolbar_back_toolbar)
    Toolbar toolbarBackToolbar;
    @Bind(R.id.shield_recycler)
    RecyclerView shieldRecycler;
    private String TAG = "ShieldActivity";
    private ShieldAdapter adapter;
    private int page;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_shield);
        ButterKnife.bind(this);
        LinearLayoutManager manager=new LinearLayoutManager(ShieldActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        shieldRecycler.setLayoutManager(manager);
        shieldRecycler.setItemAnimator(new DefaultItemAnimator());
        toolbarBackTitle.setText(getString(R.string.widget_shield));
    }

    @Override
    public void initList() {
        super.initList();
        shieldRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                SimpleUserInfo simpleUserInfo=adapter.getData().get(i);
                startUserActivity(simpleUserInfo);
            }
        });
        shieldRecycler.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void SimpleOnItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                SimpleUserInfo simpleUserInfo=adapter.getData().get(i);

                NiHaoOkgo.shieldInfo("shieldoff",simpleUserInfo.uid)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<NiHaoGeneral<String>>() {
                            @Override
                            public void call(NiHaoGeneral<String> stringNiHaoGeneral) {
                                if(stringNiHaoGeneral.status){
                                    showCanDialog(getString(R.string.message_shield2_success));
                                    page=0;
                                    requestInfo();
                                }
                            }
                        }, new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                Log.e(TAG, "错误信息" + throwable.getMessage());
                                showToast(getString(R.string.message_no_internet));
                            }
                        });

            }
        });

    }

    @Override
    public void initData() {
        super.initData();
        requestInfo();
    }

    private void requestInfo() {
        NiHaoOkgo.shieldListInfo(page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseInfo<SimpleUserInfo>, List<SimpleUserInfo>>() {
                    @Override
                    public List<SimpleUserInfo> call(BaseInfo<SimpleUserInfo> simpleUserInfoBaseInfo) {
                        Log.e(TAG,"请求到的"+simpleUserInfoBaseInfo.status+"-大小--"+simpleUserInfoBaseInfo.data.size());
                        return simpleUserInfoBaseInfo.data;
                    }
                })
                .subscribe(new Action1<List<SimpleUserInfo>>() {
                    @Override
                    public void call(List<SimpleUserInfo> simpleUserInfos) {
                        if(adapter==null){
                            adapter=new ShieldAdapter(simpleUserInfos);
                            adapter.isFirstOnly(false);
                            adapter.setOnLoadMoreListener(ShieldActivity.this);
                            shieldRecycler.setAdapter(adapter);
                        }else{
                            if(page==0){
                                adapter.setNewData(simpleUserInfos);
                            }else{
                                adapter.addData(simpleUserInfos);
                                if(simpleUserInfos.size()<15){
                                    adapter.loadComplete();
                                }
                            }
                        }
                    }
                });
    }


    @OnClick(R.id.toolbar_back_left)
    public void onClick() {
        finish();
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        requestInfo();
    }

    private void startUserActivity(SimpleUserInfo info) {
        Uinfo uinfo=new Uinfo();
        uinfo.uid=info.uid;
        Intent intent=new Intent(ShieldActivity.this, UserMineActivity.class);
        Bundle bundle=new Bundle();
        bundle.putParcelable("info",uinfo);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
